######################
###     Appendix   ###
###    Figure B2   ###
###  Nov. 28, 2017 ###
### Yuta KAMAHARA  ###
### (Yokohama Nat'l### 
###      Univ.)    ###
######################

# barplot
# This R script is for stacked bar chart.
# labels
labels <- c("Abroad/Indigenous", "Apportionment", "Districting")
i_lab <- c("ITA1996", "ITA2001", "ITA2006", "ITA2008")
t_lab <- c("TWN1998", "TWN2001", "TWN2004", "TWN2008")


###############
###         ###
###  a-->0  ###
###         ###
###############

#Italy
it0 <- read.csv("a_0.csv", header=TRUE)
i0 <- it0[-1,2:5]
colnames(i0)<-i_lab

i0 <- cbind(
  ITA1996 = c(i0[1,1], i0[2,1], i0[3,1]),
  ITA2001 = c(i0[1,2], i0[2,2], i0[3,2]),
  ITA2006 = c(i0[1,3], i0[2,3], i0[3,3]),
  ITA2008 = c(i0[1,4], i0[2,4], i0[3,4])
)
i0

#Taiwan
t0 <- it0[-1,6:9]
colnames(t0)<-t_lab
t0 <- cbind(
  TWN1998 = c(t0[1,1], t0[2,1], t0[3,1]),
  TWN2001 = c(t0[1,2], t0[2,2], t0[3,2]),
  TWN2004 = c(t0[1,3], t0[2,3], t0[3,3]),
  TWN2008 = c(t0[1,4], t0[2,4], t0[3,4])
)

# Italy & Taiwan
pdf("a0.pdf")
par(oma = c(2, 2, 0, 0)) #margin
par(mfrow = c(2,2))      # 2 by 2 matrix
par(mar = c(3, 3, 2, 1)) # the level of margin for x lines [lower, left, upper, right]
barplot(i0, legend.text = labels, 
        args.legend = list(x = 3, y = max(colSums(t0))),
        ylim=c(0,0.07), cex.names=0.8   
)
mtext(text=expression(paste(alpha,"-divergence (", alpha %->% 0, ')')),side = 2, line = 3)
barplot(t0, legend.text = NULL,
        ylim=c(0,0.07), cex.names=0.8
)
barplot(
  sweep(i0, 2, 100 / colSums(i0), "*"), cex.names=0.8
)
mtext(text="%",side = 2, line = 3)
barplot(
  sweep(t0, 2, 100 / colSums(t0), "*"), cex.names=0.8
)

mtext("Election",
      outer = TRUE,      
      side = 1,         # lower side 
      cex = 1,         
      line = 0
) 
dev.off()


###############
###         ###
###  a = -9 ###
###         ###
###############

#Italy
itm9 <- read.csv("a_-9.csv", header=TRUE)
im9 <- itm9[-1,2:5]
im9
colnames(im9)<-i_lab

im9 <- cbind(
  ITA1996 = c(im9[1,1], im9[2,1], im9[3,1]),
  ITA2001 = c(im9[1,2], im9[2,2], im9[3,2]),
  ITA2006 = c(im9[1,3], im9[2,3], im9[3,3]),
  ITA2008 = c(im9[1,4], im9[2,4], im9[3,4])
)
im9

#Taiwan
tm9 <- itm9[-1,6:9]
colnames(tm9)<-t_lab
tm9 <- cbind(
  TWN1998 = c(tm9[1,1], tm9[2,1], tm9[3,1]),
  TWN2001 = c(tm9[1,2], tm9[2,2], tm9[3,2]),
  TWN2004 = c(tm9[1,3], tm9[2,3], tm9[3,3]),
  TWN2008 = c(tm9[1,4], tm9[2,4], tm9[3,4])
)
tm9
# Italy & Taiwan
pdf("FigureB1.pdf")
par(oma = c(2, 2, 0, 0)) #margin
par(mfrow = c(2,2))      # 2 by 2 matrix
par(mar = c(3, 3, 2, 1)) # the level of margin for x lines [lower, left, upper, right]
barplot(im9, legend.text = labels, 
        args.legend = list(x = 3, y = max(colSums(im9))),
        ylim=c(0,40), cex.names=0.8   
)
mtext(text=expression(paste(alpha,"-divergence (", alpha, ' = -9)')),side = 2, line = 3)
barplot(tm9, legend.text = NULL,
        cex.names=0.8
)
barplot(
  sweep(im9, 2, 100 / colSums(im9), "*"), cex.names=0.8
)
mtext(text="%",side = 2, line = 3)
barplot(
  sweep(tm9, 2, 100 / colSums(tm9), "*"), cex.names=0.8
)

mtext("Election",
      outer = TRUE,      
      side = 1,         # lower side 
      cex = 1,         
      line = 0
) 
dev.off()

###############
###         ###
###  a = -1 ###
###         ###
###############

#Italy
itm1 <- read.csv("a_-1.csv", header=TRUE)
im1 <- itm1[-1,2:5]
im1
colnames(im1)<-i_lab

im1 <- cbind(
  ITA1996 = c(im1[1,1], im1[2,1], im1[3,1]),
  ITA2001 = c(im1[1,2], im1[2,2], im1[3,2]),
  ITA2006 = c(im1[1,3], im1[2,3], im1[3,3]),
  ITA2008 = c(im1[1,4], im1[2,4], im1[3,4])
)
im1

#Taiwan
tm1 <- itm1[-1,6:9]
colnames(tm1)<-t_lab
tm1 <- cbind(
  TWN1998 = c(tm1[1,1], tm1[2,1], tm1[3,1]),
  TWN2001 = c(tm1[1,2], tm1[2,2], tm1[3,2]),
  TWN2004 = c(tm1[1,3], tm1[2,3], tm1[3,3]),
  TWN2008 = c(tm1[1,4], tm1[2,4], tm1[3,4])
)
tm1

# Italy & Taiwan
pdf("FigureB2.pdf")
par(oma = c(2, 2, 0, 0)) #margin
par(mfrow = c(2,2))      # 2 by 2 matrix
par(mar = c(3, 3, 2, 1)) # the level of margin for x lines [lower, left, upper, right]
barplot(im1, legend.text = labels, 
        args.legend = list(x = 3, y = 0.055),
        ylim=c(0,0.055), cex.names=0.8   
)
mtext(text=expression(paste(alpha,"-divergence (", alpha, ' = -1)')),side = 2, line = 3)
barplot(tm1, legend.text = NULL,
        ylim=c(0,0.055), cex.names=0.8
)
barplot(
  sweep(im1, 2, 100 / colSums(im1), "*"), cex.names=0.8
)
mtext(text="%",side = 2, line = 3)
barplot(
  sweep(tm1, 2, 100 / colSums(tm1), "*"), cex.names=0.8
)

mtext("Election",
      outer = TRUE,      
      side = 1,         # lower side 
      cex = 1,         
      line = 0
) 
dev.off()

###############
###         ###
###  a -> 1 ###
###         ###
###############

#Italy
it1 <- read.csv("a_1.csv", header=TRUE)
i1 <- it1[-1,2:5]
i1
colnames(i1)<-i_lab

i1 <- cbind(
  ITA1996 = c(i1[1,1], i1[2,1], i1[3,1]),
  ITA2001 = c(i1[1,2], i1[2,2], i1[3,2]),
  ITA2006 = c(i1[1,3], i1[2,3], i1[3,3]),
  ITA2008 = c(i1[1,4], i1[2,4], i1[3,4])
)
i1

#Taiwan
t1 <- it1[-1,6:9]
colnames(t1)<-t_lab
t1 <- cbind(
  TWN1998 = c(t1[1,1], t1[2,1], t1[3,1]),
  TWN2001 = c(t1[1,2], t1[2,2], t1[3,2]),
  TWN2004 = c(t1[1,3], t1[2,3], t1[3,3]),
  TWN2008 = c(t1[1,4], t1[2,4], t1[3,4])
)
t1

# Italy & Taiwan
pdf("FigureB3.pdf")
par(oma = c(2, 2, 0, 0)) #margin
par(mfrow = c(2,2))      # 2 by 2 matrix
par(mar = c(3, 3, 2, 1)) # the level of margin for x lines [lower, left, upper, right]
barplot(i1, legend.text = labels, 
        args.legend = list(x = 3, y = max(colSums(t1))),
        ylim=c(0,max(colSums(t1))), cex.names=0.8   
)
mtext(text=expression(paste(alpha,"-divergence (", alpha %->% 1, ')')),side = 2, line = 3)
barplot(t1, legend.text = NULL,
        ylim=c(0,max(colSums(t1))), cex.names=0.8
)
barplot(
  sweep(i1, 2, 100 / colSums(i1), "*"), cex.names=0.8
)
mtext(text="%",side = 2, line = 3)
barplot(
  sweep(t1, 2, 100 / colSums(t1), "*"), cex.names=0.8
)

mtext("Election",
      outer = TRUE,      
      side = 1,         # lower side 
      cex = 1,         
      line = 0
) 
dev.off()

###############
###         ###
###  a = 2  ###
###         ###
###############

#Italy
it2 <- read.csv("a_2.csv", header=TRUE)
i2 <- it2[-1,2:5]
i2
colnames(i2)<-i_lab

i2 <- cbind(
  ITA1996 = c(i2[1,1], i2[2,1], i2[3,1]),
  ITA2001 = c(i2[1,2], i2[2,2], i2[3,2]),
  ITA2006 = c(i2[1,3], i2[2,3], i2[3,3]),
  ITA2008 = c(i2[1,4], i2[2,4], i2[3,4])
)
i2

#Taiwan
t2 <- it2[-1,6:9]
colnames(t2)<-t_lab
t2 <- cbind(
  TWN1998 = c(t2[1,1], t2[2,1], t2[3,1]),
  TWN2001 = c(t2[1,2], t2[2,2], t2[3,2]),
  TWN2004 = c(t2[1,3], t2[2,3], t2[3,3]),
  TWN2008 = c(t2[1,4], t2[2,4], t2[3,4])
)
t2

# Italy & Taiwan
pdf("FigureB4.pdf")
par(oma = c(2, 2, 0, 0)) #margin
par(mfrow = c(2,2))      # 2 by 2 matrix
par(mar = c(3, 3, 2, 1)) # the level of margin for x lines [lower, left, upper, right]
barplot(i2, legend.text = labels, 
        args.legend = list(x = 3, y = max(colSums(t2))),
        ylim=c(0,max(colSums(t2))), cex.names=0.8   
)
mtext(text=expression(paste(alpha,"-divergence (", alpha, ' = 2)')),side = 2, line = 3)
barplot(t2, legend.text = NULL,
        ylim=c(0,max(colSums(t2))), cex.names=0.8
)
barplot(
  sweep(i2, 2, 100 / colSums(i2), "*"), cex.names=0.8
)
mtext(text="%",side = 2, line = 3)
barplot(
  sweep(t2, 2, 100 / colSums(t2), "*"), cex.names=0.8
)

mtext("Election",
      outer = TRUE,      
      side = 1,         # lower side 
      cex = 1,         
      line = 0
) 


dev.off()


###############
###         ###
###  a = 10 ###
###         ###
###############

#Italy
it10 <- read.csv("a_10.csv", header=TRUE)
i10 <- it10[-1,2:5]
i10
colnames(i10)<-i_lab

i10 <- cbind(
  ITA1996 = c(i10[1,1], i10[2,1], i10[3,1]),
  ITA2001 = c(i10[1,2], i10[2,2], i10[3,2]),
  ITA2006 = c(i10[1,3], i10[2,3], i10[3,3]),
  ITA2008 = c(i10[1,4], i10[2,4], i10[3,4])
)
i10

#Taiwan
t10 <- it10[-1,6:9]
colnames(t10)<-t_lab
t10 <- cbind(
  TWN1998 = c(t10[1,1], t10[2,1], t10[3,1]),
  TWN2001 = c(t10[1,2], t10[2,2], t10[3,2]),
  TWN2004 = c(t10[1,3], t10[2,3], t10[3,3]),
  TWN2008 = c(t10[1,4], t10[2,4], t10[3,4])
)
t10

# Italy & Taiwan
pdf("FigureB5.pdf")
par(oma = c(2, 2, 0, 0)) #margin
par(mfrow = c(2,2))      # 2 by 2 matrix
par(mar = c(3, 3, 2, 1)) # the level of margin for x lines [lower, left, upper, right]
barplot(i10, legend.text = labels, 
        args.legend = list(x = 3, y = 0.007),
        ylim=c(0,0.007), cex.names=0.8   
)
mtext(text=expression(paste(alpha,"-divergence (", alpha, ' = 10)')),side = 2, line = 3)
barplot(t10, legend.text = NULL,
        ylim=c(0,max(colSums(t10))), cex.names=0.8
)
barplot(
  sweep(i10, 2, 100 / colSums(i10), "*"), cex.names=0.8
)
mtext(text="%",side = 2, line = 3)
barplot(
  sweep(t10, 2, 100 / colSums(t10), "*"), cex.names=0.8
)

mtext("Election",
      outer = TRUE,      
      side = 1,         # lower side 
      cex = 1,         
      line = 0
) 
dev.off()